//
//  RGTabBarViewController+RedPackets.m
//  RedGrass
//
//  Created by wukong on 2019/2/24.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGTabBarViewController+RedPackets.h"
#import <MJExtension/MJExtension.h>


@implementation RGTabBarViewController (RedPackets)

//连接服务器
-(void) connectWebSocketServer{
    
    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
    if(user && [user.token length]){
        NSString *url = [NSString stringWithFormat:@"%@%ld",URL_webSocket,(long)user.ID];
        [[SocketRocketUtility instance] SRWebSocketOpenWithURLString:url];
    }
}


-(void) showRedPacketView{
    if(self.redPacketsView){
        return;
    }
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeCenter;
    self.zh_popupController.dismissOnMaskTouched = NO;
    self.zh_popupController.allowPan = NO;
//    self.zh_popupController.slideStyle = zhPopupSlideStyleFromBottom;
    self.zh_popupController.maskAlpha = 0.2;
    RGRedPacketsView *view = [[RGRedPacketsView alloc]initWithFrame:CGRectMake(0, 0, 190, 350)];
    self.redPacketsView = view;
    view.viewDelegate = self;
    [self.zh_popupController presentContentView:view duration:0.25 springAnimated:NO];
}

- (void)SRWebSocketDidOpen {
    NSLog(@"开启成功");
    //在成功后需要做的操作。。。
}

- (void)SRWebSocketDidReceiveMsg:(NSNotification *)note {
    //收到服务端发送过来的消息
    NSString * message = note.object;
  

    NSDictionary *dicMessage = [self dictionaryWithJsonString:message];
    if([dicMessage count]){
        NSNumber *msgType = [dicMessage objectForKey:@"msgType"];
        NSString *content = [dicMessage objectForKey:@"content"];
        
        if([msgType integerValue] == 0){//
            [self showRedPacketView];
        }else if ([msgType integerValue] == -1){
            [self.redPacketsView changeViewStyleWithIsSuccess:NO money:content];
        }else if (([msgType integerValue] == 1) && content.length){
            [self.redPacketsView changeViewStyleWithIsSuccess:YES money:content];
        }
    }
}

-(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

-(void)SRWebSocketDidClose:(NSNotification *)note{
    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
    if(user && [user.token length]){
        NSString *url = [NSString stringWithFormat:@"%@%ld",URL_webSocket,(long)user.ID];
        [[SocketRocketUtility instance] SRWebSocketOpenWithURLString:url];
    }
}

#pragma mark - delegate

-(void) choseRedPacketsView:(RGRedPacketsView *)view{
    [self.zh_popupController dismiss];
    self.redPacketsView = nil;
    self.redPacketsView.viewDelegate = nil;
//    view = nil;
//    view.viewDelegate = nil;
}

-(void)gainRedPacketsView:(RGRedPacketsView *)view{

    NSString *message = [NSString stringWithFormat:@"{\"msgType\":\"1\",\"serviceType\":\"rp\"}"];
   
//    NSData *bodyData = [message dataUsingEncoding:NSUTF8StringEncoding];
    [[SocketRocketUtility instance] sendData:message];
}

@end
